package game.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameFrame extends JFrame implements KeyListener, ActionListener {
    //所有游戏界面
    int[][] arr = new int[4][4];
    //记录空白方块的位置
    int x=0, y=0;
    //统计步数
    int count=0;
    //图片路径
    String path = "JigsawPuzzles\\image\\animal\\animal3\\";
    //胜利数组
    int[][] win = new int[][] {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0},
    };
    //创建菜单选项内容
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem accountItem = new JMenuItem("公众号");
    JMenuItem animal = new JMenuItem("动物");
    JMenuItem woman = new JMenuItem("美女");
    JMenuItem spot = new JMenuItem("运动");

    public GameFrame() {
        //初始化界面
        initJFrame();
        //初始化菜单
        initJMenuBar();
        //打乱图片
        initData();
        //初始化图片
        initImage();
        this.setVisible(true);
    }

    //打乱图片并加载
    private void initData() {
        int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Random random = new Random();
        for (int i=0; i<tempArr.length; i++) {
            int index = random.nextInt(tempArr.length);
            int temp = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = temp;
        }

        for (int i = 0; i < tempArr.length; i++) {
            if (tempArr[i] == 0) {
                x = i/4;
                y = i%4;
            }
            arr[i/4][i%4] = tempArr[i];
        }
    }

    //初始化图片
    private void initImage() {
        //删除已存在的图片
        this.getContentPane().removeAll();

        //判断胜利
        if (win()) {
            JLabel win = new JLabel(new ImageIcon("JigsawPuzzles\\image\\win.png"));
            win.setBounds(203, 283, 197, 73);
            this.getContentPane().add(win);
        }

        JLabel stepCount = new JLabel("步数:"+count);
        stepCount.setBounds(50, 30, 100, 20);
        this.getContentPane().add(stepCount);

        //创建图片ImageIcon的对象
        //ImageIcon icon = new ImageIcon("D:\\桌面\\Courseware\\java\\java\\Demo\\demo1\\JigsawPuzzles\\image\\animal\\animal3\\3.jpg");
        //创建JLabel管理容器
        //JLabel jLabel = new JLabel(icon);
        //指定图片位置
        //jLabel.setBounds(0, 0, 105, 105);
        //将管理容器添加到界面当中
        //this.add(jLabel);
        //this.getContentPane().add(jLabel);
        //先加载的图片在上面，后加载在下面
        //打乱图片
        for (int i=0; i<4; i++) {
            for (int j=0; j<4; j++) {
                JLabel jLabel = new JLabel(new ImageIcon( path + arr[i][j] +".jpg"));
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);
                //设置图片斜边框，0使图凸，1凹
                jLabel.setBorder(new BevelBorder(1));
                //将管理容器添加到界面当中
                this.getContentPane().add(jLabel);
            }
        }
        //添加背景
        JLabel background = new JLabel(new ImageIcon("JigsawPuzzles\\image\\background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);
        //刷新界面
        this.getContentPane().repaint();
    }

    //初始化菜单栏
    private void initJMenuBar() {
        //设置菜单
        //创建整个菜单栏
        JMenuBar jMenuBar = new JMenuBar();
        //创建菜单栏选项
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");
        JMenu changePicture = new JMenu("更换图片");

        changePicture.add(animal);
        changePicture.add(woman);
        changePicture.add(spot);

        //将选项内容添加至选项中
        functionJMenu.add(changePicture);
        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);
        aboutJMenu.add(accountItem);
        //将选项添加至菜单栏
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

         //给选项绑定事件
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        animal.addActionListener(this);
        woman.addActionListener(this);
        spot.addActionListener(this);

        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);
    }

    //初始化框架
    private void initJFrame() {
        this.setSize(603, 680);
        //设置界面标题
        this.setTitle("拼图单机版1.0");
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置界面在屏幕居中
        this.setLocationRelativeTo(null);
        //按下关闭时结束程序
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中放置
        this.setLayout(null);
        //添加键盘点击事件
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    //键盘按下不松是调用
    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == 65) {
            //把界面中全部图片删除，并显示完整原图
            this.getContentPane().removeAll();
            JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));
            all.setBounds(83, 134, 420, 420);
            this.getContentPane().add(all);
            //添加背景
            JLabel background = new JLabel(new ImageIcon("JigsawPuzzles\\image\\background.png"));
            background.setBounds(40, 40, 508, 560);
            this.getContentPane().add(background);
            //刷新界面
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        //判断是否胜利，若胜利则结束
        if (win()) {
            return;
        }
        //对上下左右判断
        int code = e.getKeyCode();
        if (code == 37) {   //向左移动
            if (y==3){
                return;
            }
            System.out.println("向左移动");
            arr[x][y] = arr[x][y+1];
            arr[x][y+1] = 0;
            y++;
            count++;
            //调用方法加载最新图片
            initImage();
        } else if (code == 38) {    //向上移动
            if (x==3){
                return;
            }
            System.out.println("向上移动");
            arr[x][y] = arr[x+1][y];
            arr[x+1][y] = 0;
            x++;
            count++;
            //调用方法加载最新图片
            initImage();
        } else if (code == 39) {        //向右移动
            if (y==0){
                return;
            }
            System.out.println("向右移动");
            arr[x][y] = arr[x][y-1];
            arr[x][y-1] = 0;
            y--;
            count++;
            //调用方法加载最新图片
            initImage();
        } else if (code == 40) {        //向下移动
            if (x==0){
                return;
            }
            System.out.println("向下移动");
            arr[x][y] = arr[x-1][y];
            arr[x-1][y] = 0;
            x--;
            count++;
            //调用方法加载最新图片
            initImage();
        } else if (code == 65) {
            initImage();
        } else if (code == 87) {
            //作弊码
            arr = new int[][] {
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0},
            };
            initImage();
        }
    }
    //判断胜利
    public boolean win() {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != win[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Random random = new Random();
        Object obj = e.getSource();
        if (obj == replayItem) {
            count = 0;
            initData();
            initImage();
        } else if (obj == reLoginItem) {
            //返回登录界面
            this.setVisible(false);
            new LoginFrame();
        } else if (obj == closeItem) {
            System.exit(0);
        } else if (obj == accountItem) {
            JDialog jd = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("JigsawPuzzles\\image\\about.jpg"));
            jLabel.setBounds(0, 0, 258, 258);
            jd.getContentPane().add(jLabel);
            jd.setSize(344, 344);
            //设置弹框相对居中
            jd.setLocationRelativeTo(null);
            //设置置顶
            jd.setAlwaysOnTop(true);
            //不关闭则无法进行其他操作
            jd.setModal(true);
            jd.setVisible(true);
        } else if (obj == woman) {
            int i = random.nextInt(12)+1;
            path = "JigsawPuzzles\\image\\girl\\girl" + i + "\\";
            initData();
            initImage();
        } else if (obj == animal) {
            int i = random.nextInt(7)+1;
            path = "JigsawPuzzles\\image\\animal\\animal" + i +"\\";
            initData();
            initImage();
        } else if (obj == spot) {
            int i = random.nextInt(9)+1;
            path = "JigsawPuzzles\\image\\sport\\sport" + i +"\\";
            initData();
            initImage();
        }
    }
}
